package com.huang.springboot.config;

import org.springframework.util.StringUtils;
import org.springframework.web.servlet.LocaleResolver;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Locale;

/**
 * 自定义地区解析器
 * @author yili_gi
 */
public class MyLocaleResolver implements LocaleResolver {
    @Override
    public Locale resolveLocale(HttpServletRequest request) {
        //获取语言中的请求参数
        String language = request.getParameter("lang");
        //如果没有参数就使用默认的
        Locale locale = Locale.getDefault();
        //如果链接携带了国际化的参数
        if (!StringUtils.isEmpty(language)) {
            String[] split = language.split("_");
            //字符串"zh_CN",通过字符”_“分割   分割为字符数组split[0]:"zh",split[1]:"CN"
            locale = new Locale(split[0], split[1]);
            //填入语言区,国家（地区）
        }
        return locale;
    }

    @Override
    public void setLocale(HttpServletRequest request, HttpServletResponse response, Locale locale) {
    }
}
